package com.hamster.crmpre.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crmpre.config.LoginInterceptor;
import com.hamster.crmpre.mapper.*;
import com.hamster.crmpre.pojo.*;
import com.hamster.crmpre.service.BranchEnterCustomerService;
import com.hamster.crmpre.service.PhoneUserService;
import com.hamster.crmpre.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 事业部为分公司录入客户
 * @author Tarry
 * @create 2020/1/7 20:50
 */
@Service
public class BranchEnterCustomerServiceImpl implements BranchEnterCustomerService {
    @Autowired
    private CustomerRecordMapper customerRecordMapper; //购买记录mapper
    @Autowired
    private CustomerMapper customerMapper;  //客户mapper
    @Autowired
    private MessageBuyVerifyMapper messageBuyVerifyMapper; //开发客户审核消息mapper
    @Autowired
    private PersonalMessageMapper personalMessageMapper;
    @Autowired
    private CareerProfitConfigMapper careerProfitConfigMapper;
    @Autowired
    private PhoneUserService phoneUserService;
    @Override
    public void notify(String companyId, CustomerRecord customerRecord) {
        //TODO 事业部为分公司录入客户 ，分公司拿提成，录入的员工拿不拿？ 是不是录入的新客户的负责人就是 该员工
        //分公司对象
        Customer company = customerMapper.selectById(companyId);


        Date date = new Date();
        customerRecord.setId(null);//插入数据前置空
        customerRecord.setInputTime(date);
        String userName = phoneUserService.queryNameByPhone(LoginInterceptor.getLoginUser().getPhone());
//        customerRecord.setInputUser(LoginInterceptor.getLoginUser().getName());
        customerRecord.setInputUser(userName);
        customerRecord.setDel("0");         //记录状态
        customerRecord.setOrderState("0");  //订单状态  0待审核  1合格   2 不合格
        customerRecord.setRegdate(date);    //报名购买时间
        customerRecord.setDeveloper(company.getName());//分公司姓名
        customerRecord.setDeveloperId(company.getId());//分公司id
        customerRecord.setBranchCompanyId(company.getId());//分公司的id
        customerRecord.setType("2");//事业部为分公司录入客户
        customerRecord.setDivisionEmployeeId(LoginInterceptor.getLoginUser().getUserId());//事业部为分公司录入的员工的id
        customerRecordMapper.insert(customerRecord);//存入购买记录表
        MessageBuyVerify messageBuyVerify = new MessageBuyVerify();
        messageBuyVerify.setCustomerRecordId(customerRecord.getId());//设置id
        messageBuyVerify.setContent(JsonUtils.serialize(customerRecord));//设置内容
        messageBuyVerify.setState("0");
        //根据通用提成设置好的提成进行计算。
        CareerProfitConfig careerProfitConfig =  careerProfitConfigMapper.selectById("1");
        Double employeeProfit = careerProfitConfig.getCareerProfit(); //为分公司录入客户的，事业部员工的 提成比例
        messageBuyVerify.setMoney(customerRecord.getMoney() * employeeProfit);
        //存入具体的开发客户审核消息表
        messageBuyVerifyMapper.insert(messageBuyVerify);
        String verifyId = messageBuyVerify.getId();  //获取审核消息详情的id

        //在个人消息表中插入该记录
        PersonalMessage personalMessage = new PersonalMessage();

        personalMessage.setDel("0");
        personalMessage.setInputTime(date);
        personalMessage.setMessageType("开发客户");
        personalMessage.setMessageLink(verifyId);//存入对应类型消息表的id
        personalMessage.setCreateTime(date);

        //消息发送人，接收人
        personalMessage.setSenderId(customerRecord.getDeveloperId());//设置发送者id
        personalMessage.setSenderName(customerRecord.getDeveloper());//设置发送者姓名
        personalMessage.setReceiverName("财务部");
        personalMessage.setReceiverId("财务部");
        personalMessage.setDepartment("财务部");
        personalMessageMapper.insert(personalMessage);
    }

    /**
     * 为 分公司 补交尾款
     * @param id    分公司id
     * @param money 尾款金额 本次补交金额
     */
    @Override
    public void payTheBalance(String id, Double money) {

        customerMapper.updatePayTheBalance(id,money);

    }


}
